package org.geektimes.projects.user.validator.bean.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;
import java.util.regex.Pattern;

public class PasswordValidAnnotationValidator implements ConstraintValidator<PasswordValid,String> {


    private static final Pattern id_pattern = Pattern.compile("[\\dA-Za-z!@#$%^&*,.]{6,32}");

    private String message;
    private int min;
    private int max;

    @Override
    public void initialize(PasswordValid annotation) {
        this.message = annotation.message();
        this.min = annotation.min();
        this.max = annotation.max();
    }

    @Override
    public boolean isValid(String pwd, ConstraintValidatorContext context) {
        context.disableDefaultConstraintViolation();
        if (Objects.nonNull(pwd) && (pwd.length() >= min && pwd.length() <= max)) {
            return true;
        }
        context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
        return false;
    }
}
